package controller

import (
	"context"
	"net/http"
	"web/global"
	"web/proto/myregister"
	"web/utils"

	"github.com/gin-gonic/gin"
	"github.com/micro/micro/v3/service"
)

//注册方法
type RegStu struct {
	Mobile   string `json:"mobile"`
	PassWord string `json:"password"`
	SmsCode  string `json:"sms_code"`
}

//注册业务
func PostRet(ctx *gin.Context) {
	var reg RegStu
	resp := make(map[string]string)
	defer ctx.JSON(http.StatusOK, resp)
	err := ctx.Bind(&reg)

	//校验数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "校验数据错误:", err)

		return
	}
	srv := service.New()

	client := myregister.NewRegisterService("register", srv.Client())

	rsp, err := client.Register(context.Background(), &myregister.RegRequest{
		Mobile:   reg.Mobile,
		Password: reg.PassWord,
		SmsCode:  reg.SmsCode,
	})
	//获取数据库数据
	if err != nil {
		global.GlobalLog.Error(context.Background(), "注册错误:", err)
		resp["errno"] = utils.RECODE_DBERR
		resp["errmsg"] = utils.RecodeText(utils.RECODE_DBERR)
		return
	}
	//把数据返回给前端
	resp["errno"] = rsp.Errno
	resp["errmsg"] = rsp.Errmsg
}
